package com.blogs.service.comment.impl;

import com.blogs.common.constant.ErrorConstant;
import com.blogs.common.exception.BusinessException;
import com.blogs.common.utils.DateKit;
import com.blogs.common.utils.TaleUtils;
import com.blogs.domain.db.Comment;
import com.blogs.domain.db.Content;
import com.blogs.domain.dto.cond.CommentCond;
import com.blogs.mapper.CommentMapper;
import com.blogs.service.article.ContentService;
import com.blogs.service.comment.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author hubiao
 */
@Service
public class CommentServiceImpl implements CommentService {

    private static final Map<String, String> STATUS_MAP = new ConcurrentHashMap<>();
    /**
     * 评论状态：正常
     */
    private static final String STATUS_NORMAL = "approved";
    /**
     * 评论状态：不不显示
     */
    private static final String STATUS_BLANK = "not_audit";

    static {
        STATUS_MAP.put( "approved", STATUS_NORMAL );
        STATUS_MAP.put( "not_audit", STATUS_BLANK );
    }

    @Resource
    private CommentMapper commentDao;
    @Resource
    private ContentService contentService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "commentCache", allEntries = true)
    public void addComment(Comment comments) {
        String msg = null;

        if (null == comments) {
            msg = "评论对象为空";
        }

        if (StringUtils.isBlank( comments.getAuthor() )) {
            comments.setAuthor( "热心网友" );
        }
        if (StringUtils.isNotBlank( comments.getEmail() ) && !TaleUtils.isEmail( comments.getEmail() )) {
            msg = "请输入正确的邮箱格式";
        }
        if (StringUtils.isBlank( comments.getContent() )) {
            msg = "评论内容不能为空";
        }
        if (comments.getContent().length() < 5 || comments.getContent().length() > 2000) {
            msg = "评论字数在5-2000个字符";
        }
        if (null == comments.getCid()) {
            msg = "评论文章不能为空";
        }
        if (msg != null) {
            throw BusinessException.withErrorCode( msg );
        }


        Content article = contentService.getArticleById( comments.getCid() );
        if (null == article) {
            throw BusinessException.withErrorCode( "该文章不存在" );
        }

        comments.setOwnerId( article.getAuthorId() );
        comments.setStatus( STATUS_MAP.get( STATUS_BLANK ) );
        comments.setCreated( DateKit.getCurrentUnixTime() );
        commentDao.addComment( comments );

        Content temp = new Content();
        temp.setCid( article.getCid() );
        Integer count = article.getCommentsNum();
        if (null == count) {
            count = 0;
        }
        temp.setCommentsNum( count + 1 );
        contentService.updateContentByCid( temp );

    }

    @Override
    @Cacheable(value = "commentCache", key = "'commentsByCId_' + #p0")
    public List<Comment> getCommentsByCId(Integer cid) {
        if (null == cid) {
            throw BusinessException.withErrorCode( ErrorConstant.Common.PARAM_IS_EMPTY );
        }
        return commentDao.getCommentByCId( cid );
    }

    @Override
    @Cacheable(value = "commentCache", key = "'commentsByCond_'+ #p1")
    public PageInfo<Comment> getCommentsByCond(CommentCond commentCond, int pageNum, int pageSize) {
        if (null == commentCond) {
            throw BusinessException.withErrorCode( ErrorConstant.Common.PARAM_IS_EMPTY );
        }
        PageHelper.startPage( pageNum, pageSize );
        List<Comment> comments = commentDao.getCommentsByCond( commentCond );
        PageInfo<Comment> pageInfo = new PageInfo<>( comments );
        return pageInfo;
    }

    @Override
    @Cacheable(value = "commentCache", key = "'commentById_' + #p0")
    public Comment getCommentById(Integer coid) {
        if (null == coid) {
            throw BusinessException.withErrorCode( ErrorConstant.Common.PARAM_IS_EMPTY );
        }
        return commentDao.getCommentById( coid );
    }

    @Override
    @CacheEvict(value = "commentCache", allEntries = true)
    public void updateCommentStatus(Integer coid, String status) {
        if (null == coid)
            throw BusinessException.withErrorCode( ErrorConstant.Common.PARAM_IS_EMPTY );
        commentDao.updateCommentStatus( coid, status );
    }
}
